package com.wd.jetpack.bean

/**
 * NA<BR></BR>
 *
 * @author w4425
 * @version [V0.0.1, 2021/6/1]
 * @since V0.0.1
 */


val User.formattedName: String
    get() {
        return if (lastName != null) {
            if (firstName != null) {
                "$firstName $lastName"
            } else {
                lastName ?: "Unknown"
            }
        } else {
            firstName ?: "Unknown"
        }
    }

object Repository {

    //    private val _users = mutableListOf(User("Jane", ""), User("John", null), User("Anne", "Doe"))
    private val _users = mutableListOf<User>()
    val users: List<User>
        get() = _users

    val formattedUserNames: List<String>
        get() {
            return users.map { user ->
                user.formattedName
            }
        }

    init {
        val user1 = User("Jane", "")
        val user2 = User("John", null)
        val user3 = User("Anne", "Doe")

        _users.apply {
            add(user1)
            add(user2)
            add(user3)
        }
    }
}
/**
 * companion object == static
 * 单一实例模式
 * java：    public Repository getInstance()
 * Kotlin:
 * 1 class --class Repository  companion object
 *  companion object {
 *  private var INSTANCE: Repository? = null
 *  val instance: Repository
 *  get() {
 *  if (INSTANCE == null) {synchronized(Repository::class.java) {
 *  if (INSTANCE == null) {
 *  INSTANCE = Repository()}}}
 *  return INSTANCE!!}}
 * 2 通过将 class 关键字替换为 object 移除私有构造函数和伴生对象（companion object）。object Repository
 *
 */

/**
 * // Java
 * name = user.getFirstName() + " " + user.getLastName();
 * // Kotlin
 * name = "${user.firstName} ${user.lastName}"
 */